package avicit.bdp.dgs.standard.rest;

import avicit.bdp.dgs.standard.dto.LookupTableValueDto;
import avicit.bdp.dgs.standard.service.LookupTableValueService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mayi 码表管理
 */
@RestController
@RequestMapping("/api/bdp/dgs/dataStandard/lookupTableValue/")
@Api(tags = "值域标准代码值管理")
public class LookupTableValueRest {

  private final LookupTableValueService lookupTableValueService;

  @Autowired
  public LookupTableValueRest(LookupTableValueService lookupTableValueService) {
    this.lookupTableValueService = lookupTableValueService;
  }

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "码表ID",
        name = "lookupTableId",
        dataType = "String",
        paramType = "query",
        required = true),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "状态", name = "status", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<LookupTableValueDto>> getPageList(
      @RequestParam String lookupTableId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<LookupTableValueDto>> responseMsg = new ResponseMsg<>();

    QueryRespBean<LookupTableValueDto> queryRespBean =
        lookupTableValueService.getPageList(lookupTableId, keyWords, pageNo, pageSize);

    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "批量新增")
  @PostMapping("batchSave")
  public ResponseMsg<Boolean> batchSave(@RequestBody List<LookupTableValueDto> list) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    lookupTableValueService.batchSave(list);
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @ApiOperation(value = "批量删除")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @DeleteMapping
  public ResponseMsg<Boolean> batchDelete(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(lookupTableValueService.batchDelete(ids));
    return responseMsg;
  }

  @ApiOperation(value = "查询码值列表")
  @ApiParam(value = "码表ID", name = "lookupTableId", required = true)
  @GetMapping("/getValueList")
  public ResponseMsg<List<String>> getValueList(@RequestParam String lookupTableId) {
    ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(lookupTableValueService.getValueList(lookupTableId));
    return responseMsg;
  }
}
